package cn.thrjit.autoconfigure;

import cn.dev33.satoken.stp.StpInterface;
import cn.hutool.json.JSONNull;
import cn.hutool.json.JSONUtil;
import cn.thrjit.properties.SaTokenSettingProperties;
import cn.thrjit.rocketmq.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.cache.CacheProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.security.Permission;
import java.util.ArrayList;
import java.util.List;

/**
 * 自定义权限加载接口实现类
 */
@Component
@EnableConfigurationProperties({SaTokenSettingProperties.class})
public class StpInterfaceImpl implements StpInterface {
    @Autowired
    private RedisTemplate redisTemplate;// 交给spring管理redisTemplate
    @Autowired
    private SaTokenSettingProperties saTokenSettingProperties;// 交给spring管理saTokenSettingProperties
    public StpInterfaceImpl(RedisTemplate redisTemplate,SaTokenSettingProperties saTokenSettingProperties) {
        this.saTokenSettingProperties = saTokenSettingProperties;
        this.redisTemplate = redisTemplate;
    }
    /**
     * 返回一个账号所拥有的权限码集合
     */
    @Override
    public List<String> getPermissionList(Object loginId, String loginType) {
        // 用loginId放入云端的拉下来的模板替换%s占位符并从配置中获取权限码的key
        String key = String.format(saTokenSettingProperties.getPermissionKey(),loginId);
        // 从redis中通过key获取权限码
        Object permission = redisTemplate.opsForValue().get(key);
        // 把权限转换为集合
        List<String> list = JSONUtil.toList(JSONUtil.parseArray(permission), String.class);
        return list;
    }

    /**
     * 返回一个账号所拥有的角色标识集合 (权限与角色可分开校验)
     */
    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return null;
    }

}